<?php

/**
 * Description of EntityLoader
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace OverFish\Services;

use Nette\DI\Container;

class EntityLoader {
    /** @var Nette\DI\Container */
    private $entityContainer;

    /** @var array */
    private $entities = array();
    
    public function __construct(Container $container)
    {
        $modelContainer = new Container;
        $modelContainer->addService('database', $container->database);
        $modelContainer->addService('cacheStorage', $container->cacheStorage);
        $modelContainer->addService('session', $container->session);
        $modelContainer->freeze();
        $this->entityContainer = $modelContainer;
    }
    
    public function getEntity($name)
    {
        $lname = strtolower($name);

        if (!isset($this->entities[$lname])) {
            $class = 'OverFish\\Model\\Entities\\' . ucfirst($name);

            if (!class_exists($class)) {
                throw new \InvalidArgumentException("Entity '$class' not found");
            }

            
            $this->entities[$lname] = new $class();
        }

        return $this->entities[$lname];
    }

    public function __get($name)
    {
        return $this->getEntity($name);
    }
}

